<!-- 新增咨询分类 -->
<template>
    <a-modal :title="title" :visible="visible" @ok="handleOk" @cancel="visible = false" :maskClosable="false"
        destroyOnClose>
        <a-form-model ref="ruleForm" :model="form" :label-col="labelCol" :wrapper-col="wrapperCol">
            <a-form-model-item label="选择分类">
                <a-cascader :default-value="[(form.optionsId || '').split('/').map(m => Number(m))]"
                    :fieldNames="{ label: 'module', value: 'id', children: 'children' }" :options="sysOptions"
                    @change="handleCascaderChange" placeholder="请选择" />
            </a-form-model-item>
            <a-form-model-item label="分类排序">
                <a-input-number v-model="form.sort" :min="1" :precision="0" style="width: 275px" />
            </a-form-model-item>
        </a-form-model>
    </a-modal>
</template>

<script>
import { getSysOptionsById, addConfigAppInformation, editConfigAppInformation } from '@/api/web/web'
export default {
    components: {},
    data() {
        return {
            title: '新增资讯',
            type: 'add',
            labelCol: { span: 4 },
            wrapperCol: { span: 14 },
            visible: false,
            form: {
                id: undefined,
                optionsId: undefined,
                optionsName: undefined,
                sort: undefined
            },
            loading: false,
            sysOptions: [],
        }
    },
    computed: {},
    watch: {},
    methods: {
        show(data) {
            Object.keys(this.form).forEach(k => {
                this.form[k] = data.obj[k]
            })
            this.title = data.title
            this.type = data.type
            this.visible = true
        },
        async get_sysOptionsById() {
            let res = await getSysOptionsById(1)
            this.sysOptions = (res.data || []).filter(f => f.children && f.children.length)
        },
        handleCascaderChange(val, option) {
            this.form.optionsId = val.join('/')
            this.form.optionsName = option[option.length - 1].module
            console.log('this.form------>', this.form)
        },
        async handleOk() {
            let res = {}
            if (this.type === 'add') {
                res = await addConfigAppInformation({ ...this.form })
            } else {
                res = await editConfigAppInformation({ ...this.form })
            }
            let { code, msg } = res
            this.$message[code === 0 ? 'success' : 'error'](msg)
            if (code === 0) {
                this.$emit('success')
                this.visible = false
            }
        }
    },
    created() {
        this.get_sysOptionsById()
    },
    mounted() {

    },
}
</script>
<style scoped>
</style>